package ru.hh.team02.chat.client;

import static org.junit.Assert.*;

import org.junit.Test;

public class ClientRegistryTest {

    @Test
    public void shoudNumberOfRoomsBeTheSameAsSpecifiedOnInstantiation() {
        String[] roomNames = { "room1", "room2" };
        ClientRegistry clientRegistry = new ClientRegistry(roomNames);

        assertEquals(roomNames.length, clientRegistry.getRoomNames().size());

    }

    @Test
    public void shouldWeHaveTwoClientsInTheRoomOnAddingTwoClientsToTheRoom() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });

        Client client1 = new Client("Client1");
        Client client2 = new Client("Client2");

        clientRegistry.addClient(client1, "room1");
        clientRegistry.addClient(client2, "room1");

        assertEquals(2, clientRegistry.getClientsFromRoom("room1").size());
    }

    @Test
    public void shouldWeHaveNoClientsInTheRoomOnAddingClientAndImmediateTheirDeleting() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });

        Client client1 = new Client("Client1");

        clientRegistry.addClient(client1, "room1");
        clientRegistry.deleteClient(client1);

        assertEquals(0, clientRegistry.getClientsFromRoom("room1").size());
    }

    @Test
    public void shouldWeHaveOneClientMoreInTheSecondRoomAndOneClientLessInTheFirstRoomOnMovingThemFromTheFirstRoomToTheSecond() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });

        Client client1 = new Client("Client1");
        clientRegistry.addClient(client1, "room1");
        clientRegistry.changeRoom(client1, "room2");

        assertEquals(0, clientRegistry.getClientsFromRoom("room1").size());
        assertEquals(1, clientRegistry.getClientsFromRoom("room2").size());
    }

    @Test
    public void shouldWeHaveRoomThatWeAddedOnClientRegistryConstruction() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });
        
        assertTrue(clientRegistry.hasRoom("room1"));
    }

    @Test
    public void shouldWeHaveAClientWithTheNameSpecifiedAfterAddingTheClientWithThisName() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });

        Client client1 = new Client("Client1");
        clientRegistry.addClient(client1, "room1");

        assertTrue(clientRegistry.hasClientWithName("Client1"));
    }
    
    @Test
    public void shouldWeHaveNoClientWithTheNameSpecifiedAfterAddingTheClientWithThisNameAndDeletingThem() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });

        Client client1 = new Client("Client1");
        clientRegistry.addClient(client1, "room1");
        clientRegistry.deleteClient(client1);

        assertFalse(clientRegistry.hasClientWithName("Client1"));
    }

    @Test
    public void shouldWeHaveTheClientInTheRoomSpecifiedAfterAddingThemToIt() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });

        Client client1 = new Client("Client1");
        clientRegistry.addClient(client1, "room1");

        assertEquals("room1", clientRegistry.getClientRoom("Client1"));
    }
    
    @Test
    public void shouldWeHaveTheClientInTheNewRoomAfterMovingThemToIt() {
        ClientRegistry clientRegistry = new ClientRegistry(new String[] {
                "room1", "room2" });

        Client client1 = new Client("Client1");
        clientRegistry.addClient(client1, "room1");
        clientRegistry.changeRoom(client1, "room2");

        assertEquals("room2", clientRegistry.getClientRoom("Client1"));
    }

}
